import axios from 'axios'
import baseUrl from '../../util/url.js'
const detailUrl = baseUrl + 'api.php/records/detail'
const shopUrl = baseUrl + 'api.php/records/shopcar'

let state = {
    msg:'hahah',
    publicTotal:0,
    baseTotal:0,
}
let mutations = {
    publicTotalFn(state,total){
        state.publicTotal = total
    },
    baseTotalFn(state,total){
        state.baseTotal = total
    }
}
let actions = {
    postShop(context,obj) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url:shopUrl,
                data:obj
            }).then(res => {
                resolve(res.data)
            }).catch(e => {
                reject()
            })
        });
    },
    getShopList({ state, commit, dispatch }, id) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url:shopUrl,
                params: {filter:'uid,eq,'+id}
            }).then(res => {
                state.list = res.data.records
                resolve(res.data.records)
            }).catch(e => {
                reject()
            })
        });
    },
    postShopList({ state, commit, dispatch }, obj) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url:shopUrl,
                data:obj
            }).then(res => {
                resolve()
            }).catch(e => {
                reject()
            })
        });
    },
    updateShopList({ state, commit, dispatch }, obj) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'put',
                url:shopUrl + '/' + obj.id,
                data:{
                    num:obj.num
                }
            }).then(res => {
                resolve()
            }).catch(e => {
                reject()
            })
        });
    },
    getDetailData0(context,current){
        return new Promise(function(resolve,reject){
            axios({
                url:detailUrl,
                params:{
                    transform:1,
                    filter:'type,eq,注册公用设备工程师',
                    order:'id,desc',
                    page:current+',4'
                }  
            })
            .then(data=>{
                resolve(data.data.records)
                let total = Math.ceil(data.data.results/4)
                context.commit('publicTotalFn',total)
            })
        })
    },
    getDetailData1(context,current){
        return new Promise(function(resolve,reject){
            axios({
                url:detailUrl,
                params:{
                    transform:1,
                    filter:'type,eq,注册基础类',
                    order:'id,desc',
                    page:current+',4'
                }  
            })
            .then(data=>{
                resolve(data.data.records)
                let total = Math.ceil(data.data.results/4)
                context.commit('baseTotalFn',total)
            })
        })
    },
    getClassDetailData(context,type){
        return new Promise(function(resolve,reject){
            axios({
                url:detailUrl,
                params:{
                    filter:'type,eq,'+type,
                    order:'id,desc',
                    page:'1,10'
                }  
            })
            .then(data=>{
                resolve(data.data.records)
            })
        })
    },
    getList(context){
        return new Promise(function(resolve,reject){
            axios({
                url:detailUrl,
                params:{
                    order:'id,desc'
                }  
            })
            .then(data=>{
                resolve(data.data.records)
            })
        })
    }
}
export default {
    namespaced:true,
    state,
    mutations,
    actions
}